package com.thinkingstar.iads.projectcenter.serviceImpl;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.thinkingstar.iads.datacenter.entity.DcEquipmentChildrenType;
import com.thinkingstar.iads.projectcenter.entity.PcProjectEquipmentEquip;
import com.thinkingstar.iads.projectcenter.entity.PcProjectEquipmentEquipInfo;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcEquipmentEquip;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectEquipmentEquipInfo;


/**
 * 物资总库设备概算
 * @author Administrator
 *
 */
@IocBean
public class WzzkEquipmentBudgetServiceImpl {
	
	@Inject
	private DaoPcEquipmentEquip daoPcEquipmentEquip;
	
	@Inject
	private DaoPcProjectEquipmentEquipInfo daoPcProjectEquipmentEquipInfo;
	
	//物资总库设备信息
	private List<String []> liStrings = new ArrayList<>();
	
	//创建设备概算文件
	public File createWordBudget(){
		return null;
	}
	
	//根据设备库列表查询设备类型
	public Set<DcEquipmentChildrenType> getEquipmentTypeByEquipmentList(List <PcProjectEquipmentEquipInfo> equipment_equip_info_list){
		Set<DcEquipmentChildrenType> equipment_type_list = new HashSet<>();
		for(PcProjectEquipmentEquipInfo pcProjectEquipmentEquipInfo : equipment_equip_info_list){
			DcEquipmentChildrenType dc = daoPcProjectEquipmentEquipInfo.getEquipmentTypeByEquipment(pcProjectEquipmentEquipInfo);
			equipment_type_list.add(dc);
		}
		return equipment_type_list;
	}
	
	//根据某个设备配备code计算该设备配备下的所有设备的概算
	public Object findEquipmentListByCode(String project_id , String equipment_equip_code){
		PcProjectEquipmentEquip pcProjectEquipmentEquip = daoPcEquipmentEquip.findObjByCode(project_id, equipment_equip_code);
		if(pcProjectEquipmentEquip != null){
			List <PcProjectEquipmentEquipInfo> equipment_equip_info_list = daoPcProjectEquipmentEquipInfo.findEquipmentEquipInfoByEquipId(pcProjectEquipmentEquip.getId());
			Set <DcEquipmentChildrenType> equipment_type_list = getEquipmentTypeByEquipmentList(equipment_equip_info_list);
			for(DcEquipmentChildrenType dcEquipmentChildrenType : equipment_type_list){
				String [] equipment_type = new String[]{"",dcEquipmentChildrenType.getEquipment_children_type_name(),"","","",""};
				//3.设备库信息
				liStrings.add(equipment_type);
				List <PcProjectEquipmentEquipInfo> equipment_info_list = daoPcProjectEquipmentEquipInfo.findEquipmentEquipInfoByEquipIdAndEquipmentTypeId(pcProjectEquipmentEquip.getId(),dcEquipmentChildrenType.getId());
				if(!equipment_info_list.isEmpty()){
					for(PcProjectEquipmentEquipInfo pcProjectEquipmentEquipInfo : equipment_info_list){
						String [] equipment_nums = new String[]{"",pcProjectEquipmentEquipInfo.getEquipment_name(),
								"",pcProjectEquipmentEquipInfo.getEquipment_num(),
								pcProjectEquipmentEquipInfo.getEquipment_price(),getCombinedPrice(pcProjectEquipmentEquipInfo.getEquipment_num(),pcProjectEquipmentEquipInfo.getEquipment_price())};
						liStrings.add(equipment_nums);
					}
				}
			}
		}
		return liStrings;
	}
	
	//根据单价以及数量计算合价
	public String getCombinedPrice(String num , String price){
		if(StringUtils.isBlank(num) || StringUtils.isBlank(price)){
			num = "0";
			price = "0";
		}
		Double price_double = Double.valueOf(price);
		BigDecimal big_price = new BigDecimal(price_double);
		BigDecimal num_price = new BigDecimal(num);
		String result = num_price.multiply(big_price).toString();
		return getTwoDecimals(result);
	}
	
	//截取到小数点后两位小数
	public String getTwoDecimals(String result){
		if(result.indexOf(".") > -1 ){
			if(result.length() - result.lastIndexOf(".") > 3){
				return result.substring(0,result.indexOf(".")+3);
			}
		}
		return result;
	}

}
